package com.c.ctgproject.ui.files

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.c.ctgproject.mode.StorageVehicles
import com.c.ctgproject.mode.VehicleInspection
import com.c.ctgproject.retrofit.HttpHelper
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.retrofit.Service
import com.c.ctgproject.utils.file2Base64
import com.c.ctgproject.utils.handlerNull
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.io.File

class MainViewModel : ViewModel() {
    private val responsedata: MutableLiveData<Response<*>> = MutableLiveData()
    private val InitialInspectionCardata: MutableLiveData<Response<VehicleInspection>> =
        MutableLiveData()
    private val primaryInspectiondata: MutableLiveData<Response<*>> = MutableLiveData()
    private val StorageVehiclesdata: MutableLiveData<Response<StorageVehicles>> = MutableLiveData()
    private val serviece = HttpHelper.getInstance().create(Service::class.java)

    private val _errorMsg: MutableLiveData<String> = MutableLiveData()
    val errorMsg: LiveData<String> get() = _errorMsg


    fun getprimaryInspectiondata(): MutableLiveData<Response<*>> {
        return primaryInspectiondata
    }

    fun getStorageVehiclesdata(): MutableLiveData<Response<StorageVehicles>> {
        return StorageVehiclesdata
    }

    fun getresponsedata(): MutableLiveData<Response<*>> {
        return responsedata
    }

    fun getInitialInspectionCardata(): MutableLiveData<Response<VehicleInspection>> {
        return InitialInspectionCardata
    }



    //识别车牌
    fun licensePlate(token: String, file: File) {
        Log.d("日志", "上传开始照片")
        val base64= file2Base64(file)
        serviece.licensePlate(token, base64)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<*>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    _errorMsg.value = e.message
                    e.printStackTrace()
                }

                override fun onComplete() {
                    Log.d("日志", "onComplete")
                }

                override fun onSubscribe(d: Disposable) {
                    Log.d("日志", "onSubscribe")
                }

                override fun onNext(response: Response<*>) {
                    Log.d("日志", "onNext")
                    Log.d("日志response", response.handlerNull())
//                    if (response.success == "false") {
//                        Log.d("日志", "问题出现")
//                        response.data = ""
//                        response.msg = "ocr识别失败"
//                        responsedata.value = response
//                    } else {
//                        responsedata.value = response
//                    }
                    responsedata.value = response
                    Log.d("日志", "onNext结束")
                }
            })
    }

    //按车牌获取待初验车辆
    fun getInitialInspectionCar(token: String, carPlate: String) {
        serviece.getInitialInspectionCar(token, carPlate)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<VehicleInspection>> {
                override fun onError(e: Throwable) {
                    Log.d("日志错误信息", "" + e.message)
                    e.printStackTrace()
                }

                override fun onComplete() {
                    Log.d("日志错误信息", "onComplete")
                }

                override fun onSubscribe(d: Disposable) {
                    Log.d("日志错误信息", "onSubscribe")
                }

                override fun onNext(response: Response<VehicleInspection>) {
                    Log.d("日志错误信息", "onNext")
                    InitialInspectionCardata.value = response
                }
            })
    }

    //获取仓储车辆
    fun primaryInspection(token: String, carPlate: String) {
        serviece.primaryInspection(token, carPlate)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<StorageVehicles>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    e.printStackTrace()
                }

                override fun onComplete() {}
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(response: Response<StorageVehicles>) {
                    StorageVehiclesdata.value = response
                }
            })
    }

    //仓储初检
    fun primaryInspectionpost(token: String, map: Map<String, String>) {
        serviece.postprimaryInspection(token, map)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<*>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    e.printStackTrace()
                }

                override fun onComplete() {}
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(response: Response<*>) {
                    primaryInspectiondata.value = response
                }
            })
    }
}